package com.taiji.dianthus.dao.sms;

import com.taiji.dianthus.domain.sms.SmsInfo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @InterfaceName SmsInfoRepository
 * @Description
 * @Author H.M
 * @Date 2020/7/15
 */
@Repository
public interface SmsInfoRepository extends JpaRepository<SmsInfo, String>,
        JpaSpecificationExecutor<SmsInfo> {

    /**
     * 逻辑删除
     *
     * @param id
     */
    @Transactional
    @Modifying
    @Query("update SmsInfo p set p.enableFlag = 0  where p.id=:id")
    void updateFlag(@Param("id") String id);

    @Transactional
    @Modifying
    @Query("update SmsInfo p set p.msgLoc = :status  where p.id=:id")
    void updateMsgLoc(@Param("id") String id, @Param("status") Integer status);

    @Query("select info from SmsInfo info where info.enableFlag=1 and  info.msgType=2 and info.msgTime= :time ")
    List<SmsInfo> getTaskMsgByTime(@Param("time") Date time);

    @Transactional
    @Modifying
    @Query("update SmsInfo p set p.msgTime = :time  where p.id=:id")
    void editTaskTime(@Param("id") String id, @Param("time") Date time);

    @Transactional
    @Modifying
    @Query("update SmsInfo p set p.msgStatus = :status  where p.msgGroup like :msgGroup or p.retryMsgGroup1 like :msgGroup or p.retryMsgGroup2 like :msgGroup")
    void updateStatusByGroup(@Param("msgGroup") String msgGroup, @Param("status") Integer status);

    @Query("select info from SmsInfo info where info.enableFlag=1 and  (info.msgGroup like :msgGroup or info.retryMsgGroup1 like :msgGroup or info.retryMsgGroup2 like :msgGroup) ")
    SmsInfo getOneByMsgGroup(@Param("msgGroup") String msgGroup);

    @Query("select info from SmsInfo info where info.enableFlag=1 and  info.msgStatus=:msgStatus ")
    List<SmsInfo> getAllByStatus(@Param("msgStatus") int i);

    @Transactional
    @Modifying
    @Query("update SmsInfo p set p.msgStatus = :status  where p.id=:id")
    void updateMsgStatus(@Param("id") String id, @Param("status") int i);

    @Query("select t from SmsInfo t where t.smsAccountId=:accountId")
    List<SmsInfo> getIdByAccountId(Pageable pageable, @Param("accountId") String accountId);

    @Query("select t from SmsInfo t where t.createTime>=:startTime and t.createTime<:endTime and t.retryCount=:retryCount and t.enableFlag=1")
    List<SmsInfo> getAllByTime(@Param("startTime") Date start, @Param("endTime") Date end, @Param("retryCount") int retryCount);

    @Transactional
    @Modifying
    @Query("update SmsInfo p set p.retryCount = :retryCount,p.retryMsgGroup1=:smsGroup where p.id=:id")
    void updateRetryCount1(@Param("id") String id, @Param("retryCount") int retryCount, @Param("smsGroup") String smsGroup);

    @Transactional
    @Modifying
    @Query("update SmsInfo p set p.retryCount = :retryCount,p.retryMsgGroup2=:smsGroup where p.id=:id")
    void updateRetryCount2(@Param("id") String id, @Param("retryCount") int retryCount, @Param("smsGroup") String smsGroup);

    @Query("select info from SmsInfo info where info.id=:id")
    SmsInfo getOneById(@Param("id") String id);
}
